﻿//====================================================================
// Create by Daniel
// file： YCUserRoleActions.cs
// project ：YunCMS
// Build time：2012-09-22
// modify:
// description:
// ===================================================================
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using YunCMS.Model;
using YunCMS.IDAL;
//using Utility;

namespace YunCMS.DAL
{
    /// <summary>
    /// Data Access Layer of dbo.YC_User_RoleActions.
    /// </summary>
    public partial class YCUserRoleActionsDAL : IYCUserRoleActionsDAL
    {
		#region 
		
		/// <summary>
		/// constractor
		/// </summary>
		public YCUserRoleActionsDAL()
		{
		}
		#endregion

        #region -----------Methods-----------
		/// <summary>
		/// Insert a model to Database
		/// </summary>
		/// <param name="model">YCUserRoleActionsnew model</param>
		/// <returns>The new item Id</returns>
		public int Add(YCUserRoleActions model)
		{
			StringBuilder sqlStr = new StringBuilder();
			sqlStr.Append("insert into YC_User_RoleActions(");
            sqlStr.Append("RoleID,ActionID,Note)");
            sqlStr.Append(" values (");
            sqlStr.Append("@RoleID,@ActionID,@Note)");
            sqlStr.Append(";select @@IDENTITY");
			int res;

			res = SqlHelper.ExecuteNonQuery(CommandType.Text,sqlStr.ToString(),PrepareSqlParams(model, false));
			return res;
		}
		/// <summary>
		/// Insert a model to Database in transaction.
		/// </summary>
        /// <param name="trans">SqlTransaction</param>
		/// <param name="model">YCUserRoleActionsnew model</param>
		/// <returns>The new item Id</returns>
		public int Add(SqlTransaction trans,YCUserRoleActions model)
		{
			StringBuilder sqlStr = new StringBuilder();
			sqlStr.Append("insert into YC_User_RoleActions(");
            sqlStr.Append("RoleID,ActionID,Note)");
            sqlStr.Append(" values (");
            sqlStr.Append("@RoleID,@ActionID,@Note)");
			int res;			
			
			res = SqlHelper.ExecuteNonQuery(trans,CommandType.Text,sqlStr.ToString(),PrepareSqlParams(model, false));
			return res;
		}
		/// <summary>
		/// update a model to db
		/// </summary>
		/// <param name="model">YCUserRoleActions</param>
		/// <returns>rows affected</returns>
		public int Update(YCUserRoleActions model)
		{
            StringBuilder sqlStr = new StringBuilder();
			sqlStr.Append("update YC_User_RoleActions set ");
            sqlStr.Append("RoleID=@RoleID,ActionID=@ActionID,Note=@Note");
			sqlStr.Append(" where ID=@ID");
			int res;
            
			res = SqlHelper.ExecuteNonQuery(CommandType.Text,sqlStr.ToString(),PrepareSqlParams(model, true));
			return res;
		}
		
        /// <summary>
		/// update a model to db in transaction
		/// </summary>
        /// <param name="trans">SqlTransaction</param>
		/// <param name="model">YCUserRoleActions</param>
		/// <returns>rows affected</returns>
		public int Update(SqlTransaction trans,YCUserRoleActions model)
		{
            StringBuilder sqlStr = new StringBuilder();
			sqlStr.Append("update YC_User_RoleActions set ");
            sqlStr.Append("RoleID=@RoleID,ActionID=@ActionID,Note=@Note");
			sqlStr.Append(" where ID=@ID");
			int res;
	
			res = SqlHelper.ExecuteNonQuery(trans,CommandType.Text,sqlStr.ToString(),PrepareSqlParams(model, true));
			return res;
		}
		
		/// <summary>
		/// delete a model from db
		/// </summary>
	    /// <param name="id">ID</param>
		/// <returns>rows affected</returns>
		public int Delete(int id)
		{
			StringBuilder sqlStr = new StringBuilder();
            sqlStr.Append("delete YC_User_RoleActions ");
            sqlStr.Append(" where ID=@ID");
			SqlParameter[] parameters={
							new SqlParameter("@ID",SqlDbType.Int),
			
			};
			parameters[0].Value=id;
			return SqlHelper.ExecuteNonQuery(CommandType.Text,sqlStr.ToString(),parameters);
		}
		
        /// <summary>
		/// delete a model from db in transaction
		/// </summary>        
        /// <param name="trans">SqlTransaction</param>
	    /// <param name="id">ID</param>
		/// <returns>rows affected</returns>
		public int Delete(SqlTransaction trans,int id)
		{
			StringBuilder sqlStr = new StringBuilder();
            sqlStr.Append("delete YC_User_RoleActions ");
            sqlStr.Append(" where ID=@ID");
			SqlParameter[] parameters={
							new SqlParameter("@ID",SqlDbType.Int),
			
			};
			parameters[0].Value=id;
			return SqlHelper.ExecuteNonQuery(trans,CommandType.Text,sqlStr.ToString(),parameters);
		}
		
        /// <summary>
		///Get YCUserRoleActions Entity
		/// </summary>
		/// <param name="dr">dr</param>
		/// <returns>YCUserRoleActions Entity</returns>
		public YCUserRoleActions PopulateYCUserRoleActionsFromDr(IDataReader dr)
		{
			YCUserRoleActions Obj = new YCUserRoleActions();
			
				Obj.ID = Convert.ToInt32( dr["ID"]);
				Obj.RoleID = Convert.ToInt32( dr["RoleID"]);
				Obj.ActionID = Convert.ToInt32( dr["ActionID"]);
                Obj.Note =  dr["Note"].ToString();
			
			return Obj;
		}
		
		/// <summary>
		/// get a YCUserRoleActions object 
		/// </summary>
		/// <param name=id>ID</param>
		/// <returns>YCUserRoleActions object</returns>
		public YCUserRoleActions GetYCUserRoleActions (int id)
		{
			YCUserRoleActions _obj=null;
			SqlParameter[] parameters={
			new SqlParameter("@ID",SqlDbType.Int)
			};
			parameters[0].Value=id;
			string sqlStr="select * from YC_User_RoleActions where ID=@ID";
			using(SqlDataReader dr=SqlHelper.ExecuteReader(CommandType.Text,sqlStr,parameters))
			{
				while(dr.Read())
				{
            		_obj = PopulateYCUserRoleActionsFromDr(dr);
				}
			}
			return _obj;
		}
		/// <summary>
		/// Get YCUserRoleActionss List
		/// </summary>
		/// <returns>List</returns>
		public IList<YCUserRoleActions> GetList(string sqlStr)
		{
			IList< YCUserRoleActions> Obj=new List< YCUserRoleActions>();
			using(SqlDataReader dr=SqlHelper.ExecuteReader(CommandType.Text,sqlStr))
			{
				while(dr.Read())
				{
					Obj.Add(PopulateYCUserRoleActionsFromDr(dr));
				}
			}
			return Obj;
		}
		/// <summary>
        /// if exist a YCUserRoleActions according by ID
        /// </summary>
        /// <param name="id">id</param>
        /// <returns>true/false</returns>
		public bool IsExist(int id)
		{
			SqlParameter[] parameters={
                                      new SqlParameter("@ID",SqlDbType.Int)
                                  };
            parameters[0].Value=id;
            string sqlStr="select Count(1) from YC_User_RoleActions where ID=@ID";
            int a=Convert.ToInt32(SqlHelper.ExecuteScalar(CommandType.Text,sqlStr,parameters));
            if(a>0)
            {
                return true;
            }
            else
            {
                return false;
            }
		}
        
        private SqlParameter[] PrepareSqlParams(YCUserRoleActions model, bool isUpdate)
        {
     			 List<SqlParameter> parameters = new List<SqlParameter>() {							
							new SqlParameter("@RoleID",SqlDbType.Int),
							new SqlParameter("@ActionID",SqlDbType.Int),
							new SqlParameter("@Note",SqlDbType.NVarChar)
			};
                parameters[0].Value = model.RoleID;                
                parameters[1].Value = model.ActionID;                
			if(model.Note!= String.Empty)
				parameters[2].Value = model.Note;
			else
				parameters[2].Value = DBNull.Value;				
            
            if (isUpdate)
            {
                SqlParameter param = new SqlParameter("@ID",SqlDbType.Int);
                param.Value = model.ID; 
                parameters.Add(param);
            }

            return parameters.ToArray();
        }
        
		/*
		public int GetMaxId()
		{
			int maxid;
			string sqlStr="select max(ID) from YC_User_RoleActions order by ID desc";
			using(SqlDataReader dr=SqlHelper.ExecuteReader(CommandType.Text,sqlStr))
			{
				if(dr.Read())
				{
					maxid = Convert.ToInt32( dr["ID"]);
				}
				else
					maxid = 0;
			}
			return maxid;
		}

		*/
        #endregion
		
    }
}




